package com.itheima.entry.factory;


import com.itheima.entry.controller.IStatisticsController;
import com.itheima.entry.service.IStatisticsService;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;


/*
工厂类,生产需要的对象
方法全静态,方法返值类型为接口名,返回值为接口的实现类
 */
public class StatisticsFactroy {

    private static Properties prop = new Properties();

    static {
        InputStream reader = ClassLoader.getSystemResourceAsStream("factory.properties");
        try {
            prop.load(reader);
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static IStatisticsService getService() {

        IStatisticsService o=null;
        try {
            Class<?> clazz = Class.forName(prop.getProperty("IStatisticsService"));
            o = (IStatisticsService) clazz.newInstance();
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
            e.printStackTrace();
        }


        return o;
    }

    public static IStatisticsController getController() {
        IStatisticsController o =null;

        try {
            Class<?> clazz = Class.forName(prop.getProperty("IStatisticsController"));
            o = (IStatisticsController) clazz.newInstance();
        } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
            e.printStackTrace();
        }


        return o;
    }
}
